import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
import { resolve } from "path"; // pnpm i @types/node
import AutoImport from "unplugin-auto-import/vite";
import { ElementPlusResolver, AntDesignVueResolver } from "unplugin-vue-components/resolvers";
import vueJsx from "@vitejs/plugin-vue-jsx"; // 配置 JSX
import Components from "unplugin-vue-components/vite";
// 引入unocss
import unoCss from "unocss/vite";
// 预设
import { presetIcons, presetAttributify, presetUno } from "unocss";

// https://vitejs.dev/config/
export default defineConfig({
	plugins: [
		vue(),
		vueJsx(),
		Components({
			resolvers: [ElementPlusResolver(), AntDesignVueResolver()]
		}),
		AutoImport({
			resolvers: [ElementPlusResolver(), AntDesignVueResolver()],
			// targets to transform
			include: [/\.[tj]sx?$/, /\.vue$/, /\.vue\?vue/, /\.md$/],
			// global imports to register
			imports: [
				// 插件预设支持导入的api
				"vue",
				"vue-router",
				"pinia"
			],
			eslintrc: {
				enabled: false, // Default `false`
				filepath: "./.eslintrc-auto-import.json", // Default `./.eslintrc-auto-import.json`
				globalsPropValue: true // Default `true`, (true | false | 'readonly' | 'readable' | 'writable' | 'writeable')
			},
			dts: "./auto-imports.d.ts"
		}),
		unoCss({
			presets: [presetIcons(), presetAttributify(), presetUno()],
			rules: [
				["flex", { display: "flex" }],
				["green", { color: "green" }],
				["font28", { "font-size": "40px" }],
				[/^m-(\d+)$/, ([, d]) => ({ margin: `${Number(d) * 10}px` })]
			],
			// 组合样式 自定义
			shortcuts: {
				fuck: ["green", "font28"]
			}
		})
	],
	resolve: {
		alias: {
			"@": resolve(__dirname, "./src"),
			"@less": resolve(__dirname, "./src/assets/less"),
			"@icons": resolve(__dirname, "./src/assets/icons"),
			"@images": resolve(__dirname, "./src/assets/images"),
			"@http": resolve(__dirname, "./src/http"),
			"@components": resolve(__dirname, "./src/components"),
			"@pages": resolve(__dirname, "./src/pages"),
			"@router": resolve(__dirname, "./src/router"),
			"@store": resolve(__dirname, "./src/store"),
			"@utils": resolve(__dirname, "./src/utils"),
			"@publicFunctions": resolve(__dirname, "./src/publicFunctions")
		}
	},
	server: {
		hmr: true, // 开启热更新
		proxy: {
			// 这里也可以设置 open:true 但是我们开头已经在package.json配置 vite --open 效果相同
			"^/api/.*": {
				target: "你的项目根地址",
				changeOrigin: true,
				rewrite: (path) => path.replace(/^\/api/, ""),
				secure: false // https开启
			}
		}
	},
	envDir: "./src/env"
});
